package 链表;

public class NodeOperation {
private static Node pushFront(Node head, String v){
    Node node = new Node(v);
    node.next = head;
    head = node;
    return head;
}

private static Node popFront(Node head){
    if (head == null){
        throw new RuntimeException("链表为空");
    }

    head = head.next;
    return head;
}

private static Node pushBack(Node head, String v){
    if (head == null){
        Node node = new Node(v);
        return node;
    }

    Node last = head;
    while (last.next != null){
        last = last.next;
    }

    Node node = new Node(v);
    last.next = node;
    return head;
}

private static Node popBack(Node head){
    if (head == null){
        throw new RuntimeException("链表为空");
    }

    if (head.next == null){
        return null;
    }

    Node last2 = head;
    while (last2.next.next != null){
        last2 = last2.next;
    }
    last2.next = null;
    return head;
}
}
